import request from '../../../utils/request'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        mvlist:[],
        mvComment:[],
        Comment:[],
        Mvdetail:[],
        mvid:'',
        tableTabs:[
            {
                id: 0,
                value: "推荐",
                isActive: true
              },
              {
                id: 1,
                value: "最新",
                isActive: false
              },
              {
                id: 2,
                value: "最热",
                isActive: false
              }
        ],
        show:false,
        offset:1,
        tabindex:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const {mvid}=options
        this.setData({
            mvid
        })
        this.getMvlist()
        this.getMvComment()
        this.getMvdetail()
    },

    async getMvlist(){
        let mvlist = await request('/mv/url', {id: this.data.mvid});
        this.setData({
            mvlist
        })
    },

    async getMvdetail(){
        let Mvdetail = await request('/mv/detail/info', {mvid: this.data.mvid});
        console.log(Mvdetail);
        this.setData({
            Mvdetail
        })
    },
    async getMvComment(){
        let mvComment = await request('/comment/mv', {id: this.data.mvid});
        this.setData({
            mvComment
        })
        // console.log(this.data.mvComment);
    },
      getComment() {
        this.setData({ 
            show: true,
         });
         let index=0
         let tabs=this.data.tableTabs
         // console.log(index,tabs);
         tabs.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false);
         this.setData({
             Comment:'',
             offset:1,
             tableTabs:tabs,
             tabindex:index
         })
        wx.showLoading({
            title: '正在加载'
          })
        this.gettuijianCommom()
      },
      onClose() {
        this.setData({ 
            show: false,
            tabindex:this.data.tabindex
         });
      },
     
      handlechange(e){
        // console.log(e.target.id);
        let index=parseInt(e.target.id)
        let tabs=this.data.tableTabs
        // console.log(index,tabs);
        tabs.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false);
        this.setData({
            Comment:'',
            offset:1,
            tableTabs:tabs,
            tabindex:index
        })
        if(index==0){
            this.gettuijianCommom()
        }else if(index==1){
            this.getNesCommom()
        }else{
            this.gethotCommom()
        }
      },

      async  gettuijianCommom(){
        const tuijianCom1 = await request('/comment/mv', {id: this.data.mvid,offset:this.data.offset,limit:15});
        console.log(tuijianCom1);
        const tindex=this.data.offset
        if(tindex==1){
            const tuijianCom=this.data.mvComment.hotComments.slice(0,5).concat(tuijianCom1.comments) 
            this.setData({
                // Comment:tuijianCom
                Comment:[...this.data.Comment,...tuijianCom]
            })
        }else{
            this.setData({
                Comment:[...this.data.Comment,...tuijianCom1.comments]
            })
        }
        
            wx.hideLoading();
      },
      gethotCommom(){
            const hotCom=this.data.mvComment.hotComments
            this.setData({
                Comment:hotCom
            })
            wx.hideLoading();
      },
     async  getNesCommom(){
        const NewCom = await request('/comment/mv', {id: this.data.mvid,offset:this.data.offset,limit:15});
            this.setData({
                Comment:[...this.data.Comment,...NewCom.comments],
            })
            wx.hideLoading();
      },
      handleToLower(){
        const tindex=this.data.tabindex
        if(this.data.offset==1){
            this.setData({
                offset:this.data.offset+14
            })
        }else{
            this.setData({
                offset:this.data.offset+15
            })
        }
      
        if(tindex==0){
            this.gettuijianCommom()
        }else if(tindex==1){
            this.getNesCommom()
        }else{
            wx.showToast({
              title: '没了，别翻了',
            })
        }
    }
})